package com.flower.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.flower.model.entity.Put;
import com.flower.model.entity.Putstorage;
import com.flower.result.PageResult;
import com.flower.result.Result;
import com.flower.service.IPutService;
import com.github.pagehelper.Page;
import io.swagger.annotations.Api;
import jakarta.annotation.Resource;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.net.URLEncoder;
import java.util.List;

/**
 * @author lm
 * @create 2022-03-17 16:29
 */
@RestController
@RequestMapping("/put")
@Api(tags = "入库管理系统")
@Slf4j
public class PutController {

    @Autowired
    private IPutService putService;


    //新增和修改
    @PostMapping
    public Result save(@RequestBody Put put) {
        putService.saveOrUpdate(put);
        //新增或者更新
        return Result.success("操作成功");

    }

    //查询所有数据
    @GetMapping("/")
    public Result findAll() {
        return Result.success(putService.list());
    }

    //删除数据
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        putService.removeById(id);
        return Result.success("删除成功");
    }

    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        putService.removeByIds(ids);
        return Result.success("批量删除成功");
    }

    //总支出
    @GetMapping("/findsum")
    public Result sumByToday() {
        return Result.success(putService.sumByToday());
    }

    //入库量
    @GetMapping("/findwholput")
    public Result findwholput() {

        return Result.success(putService.saleValidate());
    }

    //分页查询——mybatis-plus的方式查询
    @GetMapping("/page")
    public Result<PageResult> findPage(@RequestParam(defaultValue = "") String putId,
                                       @RequestParam(defaultValue = "") String putOper,
                                       @RequestParam String startTime,
                                       @RequestParam String endTime,
                                       @RequestParam Integer pageNum,
                                       @RequestParam Integer pageSize

    ) {

        Page<Putstorage> page = putService.findPage(new Page<>(pageNum, pageSize), putId, putOper, startTime, endTime);

        return Result.success(new PageResult(page.getTotal(), page.getResult()));

    }

    /**
     * 导出接口
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response) throws Exception {
        //从数据库查询出所有的数据
        List<Put> list = putService.list();
        //通过工具类创建writer写出到磁盘路径
        //ExcelWriter writer = ExcelUtil.getWriter(filesUploadPath+ "/用户信息.xlsx");
        //在内存操作，写出到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        //自定义标题别名
        writer.addHeaderAlias("id", "编号");
        writer.addHeaderAlias("putId", "入库单号");
        writer.addHeaderAlias("putName", "入库商品");
        writer.addHeaderAlias("putDate", "入库日期");
        writer.addHeaderAlias("putSupplier", "供应商");
        writer.addHeaderAlias("putOper", "操作人");
        writer.addHeaderAlias("putNum", "入库数量");
        writer.addHeaderAlias("putUnit", "数量单位");
        writer.addHeaderAlias("putPrice", "单价");
        writer.addHeaderAlias("putTotal", "总价");
        writer.addHeaderAlias("status", "状态");

        //一次性写出list内的对象到Excel，使用默认样式，强制输出标题
        writer.write(list, true);
        //设置浏览器响应格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = URLEncoder.encode("入库单信息", "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");

        ServletOutputStream out = response.getOutputStream();
        writer.flush(out, true);
        out.close();
        writer.close();
    }
}


